﻿
/*
 * PsTFS is a set of PowerShell commands to manage and use Team Foundation Server. PowerShell 
 * effectiveness is combined with the power of TFS API to maximizes TFS capabilities.
 * 
 * Creator : Choulant Noham
 * Mail : choulant@gmail.com
 * Blog : http://choulant.blogspot.com
 * 
 */

using System;
using System.ComponentModel;
using System.Management.Automation;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Configuration.Install;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using System.IO;
using PowerShell.Host.PsTFS.CdmLet.PsObject;


namespace PowerShell.Host.PsTFS.CdmLet
{
    /// <summary>
    /// Ajout d'un label
    /// </summary>
    [Cmdlet(VerbsCommon.Add, "Label")]
    public class Add_Label : Cmdlet
    {
        public Add_Label()
        {
            if (TFSProvider.s_TFSDriveInfo != null)
            {
                this.TFS = TFSProvider.s_TFSDriveInfo.TFS;
                this.Scope = TFSProvider.s_TFSDriveInfo.ProjectName;
            }

            this.RecursionType = RecursionType.Full;
            this.Description = string.Empty;
            this.LabelName = string.Empty;
            this.Item = null;
        }
        protected override void ProcessRecord()
        {
            if (TFS == null)
            {
                WriteError(new ErrorRecord(new ArgumentException("No server TFS!!!"), "PsTFS", ErrorCategory.ObjectNotFound, TFS));
                return;
            }
            if (Scope == null)
            {
                WriteError(new ErrorRecord(new ArgumentException("No server Scope!!!"), "PsTFS", ErrorCategory.ObjectNotFound, TFS));
                return;
            }
            if (this.Scope.IndexOf('$') < 0)
            {
                this.Scope = "$/" + this.Scope;
            }
            //Récupération du version control de tfs 
            VersionControlServer versionControl = PsService.VCS(this.TFS);

            //Récupération du version control pour le label
            VersionControlLabel vcl = new VersionControlLabel(versionControl
                , this.LabelName
                , versionControl.AuthenticatedUser
                , this.Scope
                , this.Description);

            //Création du tableau des item sur les quel mettre un label
            LabelItemSpec[] labelItemSpecs = new LabelItemSpec[this.Item.Length];
            for (int i = 0; i < this.Item.Length; i++)
            {
                ItemSpec itemSpec = new ItemSpec(this.Item[i], this.RecursionType);
                labelItemSpecs[i] = new LabelItemSpec(itemSpec, VersionSpec.Latest, false);
            }

            //Construction du label
            LabelResult[] result = versionControl.CreateLabel(vcl, labelItemSpecs, LabelChildOption.Replace);

            //Information
            PsLog.AddLog("Successfully created label " + vcl.Name);

            //Renvois les informations
            WriteObject(result);
        }

        /// <summary>
        /// Permet d'informer la fin de la commande
        /// </summary>
        protected override void EndProcessing()
        {
            PsLog.AddLog(null, "Add-Label is end");
            base.EndProcessing();
        }

        /// <summary>
        /// Object contenant Team Foundation Server 2008
        /// </summary>
        [Parameter(Position = 0)]
        public TeamFoundationServer TFS { get; set; }

        /// <summary>
        /// Nom du label
        /// </summary>
        [Parameter(Position = 1, Mandatory = true)]
        [ValidateNotNullOrEmpty]
        public string LabelName { get; set; }

        /// <summary>
        /// Projet sur le quel mettre le label
        /// </summary>
        [Parameter(Position = 2)]
        public string Scope { get; set; }

        /// <summary>
        /// Description du label
        /// </summary>
        [Parameter(Position = 3, Mandatory = true)]
        [ValidateNotNullOrEmpty]
        public string Description { get; set; }

        /// <summary>
        /// Possibilité  full, OneLenvel, none
        /// </summary>
        [Parameter(Position = 4, Mandatory = true)]
        [ValidateNotNullOrEmpty]
        public RecursionType RecursionType { get; set; }

        /// <summary>
        /// Elements à labeliser
        /// </summary>
        [Parameter(Position = 5, Mandatory = true)]
        [ValidateNotNullOrEmpty]
        public string[] Item { get; set; }
    }
}
